package view;

import java.awt.*;
import java.awt.event.*;

import javax.swing.event.*;
import beans.*;
import java.util.*;
import javax.swing.*;


public class BuscaTutorialView implements IBuscaTutorialView{

	private ActionListener listener = null;
	private Vector<Tutorial> tutorial = null;
	private String action = null;
		
	private JList  kList=null;
	private DefaultListModel lista=null;
	private JPanel BackPanel = null;
	private JScrollPane scrollPane=null;
	
//	public BuscaTutorialView(JPanel BackPanel) {
//		this.BackPanel = BackPanel;
//		tutorial=new Vector<BuscaTutorial>();
//		buildGUI();
//		
//	}
//	
//	public BuscaTutorialView(JPanel BackPanel, Vector<BuscaTutorial> tutorial) {
//		this.tutorial = tutorial;
//		this.BackPanel = BackPanel;
//		buildGUI();
//	}
	
	public BuscaTutorialView() {
		tutorial=new Vector<Tutorial>();
		BackPanel = new JPanel();
		BackPanel.setLayout(new BoxLayout(BackPanel, BoxLayout.Y_AXIS));
		BackPanel.setPreferredSize(new Dimension(120, 0));
		scrollPane = new JScrollPane();
		lista=new DefaultListModel();
		buildGUI();
	}
	
	public JPanel getPanel() {
		return BackPanel;
	}
	
	@Override
	public void show() throws Exception {
		BackPanel.setVisible(true);
	}	public BuscaTutorialView(JPanel BackPanel) {
		this.BackPanel = BackPanel;
		tutorial=new Vector<Tutorial>();
		buildGUI();
		
	}
	
	public BuscaTutorialView(JPanel BackPanel, Vector<Tutorial> tutorial) {
		this.tutorial = tutorial;
		this.BackPanel = BackPanel;
		buildGUI();
	}

	@Override
	public void refresh() {
		
	}

	@Override
	public void hide() {
		BackPanel.setVisible(false);
	}

	@Override
	public void setActionListener(ActionListener listener) {
		this.listener = listener;
	}

	@Override
	public Vector<Tutorial> getBuscaTutorial() {
		return tutorial;
	}

	@Override
	public void setBuscaTutorial(Vector<Tutorial> tutorial) {
		this.tutorial=tutorial;
	}

	public BuscaTutorialView getView() {
		return this;
	}
	
	@Override
	public void showError(Exception e) {
		JOptionPane.showMessageDialog(BackPanel, e.toString(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
	}
	
	public void addTutorial(Tutorial topico) {
		tutorial.add(topico);
		addListaBuscaTutorial();
	}
	
	//private void buildGUI(JPanel BackPanel) {
	private void buildGUI() {
		//addListaBuscaTutorial();
		kList= new JList(lista);
		scrollPane.add(kList);
		BackPanel.add(scrollPane);
		kList.addListSelectionListener(new SelecaoListaListener());
		BackPanel.setVisible(true);
		scrollPane.setVisible(true);
		kList.setVisible(true);
	}
	
	private DefaultListModel addListaBuscaTutorial () {
		lista.clear();
		for(int i=0;i<tutorial.size();i++)
			lista.addElement(tutorial.get(i).getNome());
		return lista;
	}

	private class SelecaoListaListener implements ListSelectionListener {
		@Override
		public void valueChanged(ListSelectionEvent e) {
			action=tutorial.get(1).ACT_UPDATE;
			listener.actionPerformed(new ActionEvent(getView(),tutorial.get(1).ID_TOPICO, action));
		}			
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			
			SwingUtilities.invokeLater(new Runnable() {
				//String [] tp1={"Topico1","Topico2"};
				//String [] tp2={"Topico3","Topico4"};
				//String [] tp3={"Topico5","Topico6"};
				
				
				public void run() {
					BuscaTutorialView o=new BuscaTutorialView();
					Tutorial tutorial=new Tutorial();
					tutorial.setNome("Topico de teste");
					o.addTutorial(tutorial);
					try {
						JFrame n= new JFrame();
						//n.setContentPane(o.getPanel());
						n.add(o.getPanel());
						n.setSize(816, 478);
						n.setLayout(new FlowLayout());
						n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						n.setVisible(true);
						}
					catch (Exception e) { e.printStackTrace();}
				}
			});
		}
		catch (ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		catch ( Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setActionEventListener(ActionListener listener) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setBuscaTutorialView(BuscaTutorialView principalView) {
		// TODO Auto-generated method stub
		
	}
}
